package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.icd.service.IcdDiseaseService;

@Controller
public class IcdDiseaseController {

	@Autowired
	private IcdDiseaseService icdDiseaseService;
	@Autowired
	private IcdCodeController icdCodeController;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameSearch");
		String categorySearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "categorySearch");
		String exactSearchFlagFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdDiseaseService.list(Integer.parseInt(start), nameSearch, categorySearch, exactSearchFlagFlag,
				countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_check_former_name.do")
	public ReturnData checkFormerName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		ReturnData returnData = icdDiseaseService.checkFormerName(name);
		if (ReturnCode.SUCCESS.equals(returnData.getReturnCode())) {
			return icdCodeController.edit(req, resp);
		}
		return returnData;
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_get_category_by_name.do")
	public ReturnData getCategoryByName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String id = req.getParameter("id");
		String type = req.getParameter("type");
		return icdDiseaseService.getCategoryByName(id, name, type);
	}

	@RequestMapping(value = "/icd/disease_export_sex_age.htm")
	public void exportSexAge(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		icdDiseaseService.exportSexAge(resp);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_sex_age_list.htm")
	public ModelAndView diseaseSexAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String sexKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "sexKey");
		String tabIdFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "tabid");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdDiseaseService.sexAgeList(Integer.parseInt(start), keyword, sexKey, tabIdFlag, countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_set_sex.do")
	public ReturnData setSex(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String sex = req.getParameter("sex");
		return icdDiseaseService.setSex(id, sex, user);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_set_age.do")
	public ReturnData setAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String age = req.getParameter("age");
		Boolean flag = Boolean.parseBoolean(req.getParameter("flag"));
		return icdDiseaseService.setAge(id, age, flag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_submit_sex_age.do")
	public ReturnData submitSexAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String ids = req.getParameter("ids");
		return icdDiseaseService.submitSexAge(ids, user);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_set_urgent_flag.do")
	public ReturnData setUrgentFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String urgentFlag = req.getParameter("urgentFlag");
		return icdDiseaseService.setUrgentFlag(id, urgentFlag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_set_check_category.do")
	public ReturnData setCheckCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String checkCategory = req.getParameter("checkCategory");
		return icdDiseaseService.setCheckCategory(id, checkCategory, user);
	}

	@RequestMapping(value = "/icd/disease_export.htm")
	public void diseaseExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		icdDiseaseService.diseaseExport(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_import.do")
	public ReturnData diseaseImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		return icdDiseaseService.diseaseImport(file, user);
	}
}
